{extend name="$base" /}
{block name="resources"}
<link rel="stylesheet" type="text/css" href="ADMIN_CSS/product.css">
<style type="text/css">
.ns-main{margin-top:0;}
</style>
{/block}
{block name="main"}
<div class="options-btn">
	<button class="btn-common-delete" onclick="batchDelete()">批量删除</button>
	<button class="btn-common" onclick="location.href='{:__URL('ADMIN_MAIN/config/adddocument')}';">添加帮助内容</button>
</div>
<table class="table-class">
	<colgroup>
		<col style="width: 2%">
		<col style="width: 60%;">
		<col style="width: 19%;">
		<col style="width: 4%;">
		<col style="width: 15%">
	</colgroup>
	<thead>
		<tr align="center">
			<th><i class="checkbox-common"><input type="checkbox" ></i></th>
			<th align="left">标题</th>
			<th>分类</th>
			<th>排序</th>
			<th>操作</th>
		</tr>
	</thead>
	<tbody id="linkList"></tbody>
</table>
{/block}
{block name="script"}
<script type="text/javascript">
$(function(){
	$("#linkList input[type='number']").live("keyup",function(){
		if($(this).val().length>0){
			var reg = /^[0-9]+$/;
			if(!reg.test($(this).val())){
				$(this).val(0);
			}
			if($(this).val().indexOf(".")>-1){
				$(this).val(0);
			}
		}
	});
});


//分页数据
function LoadingInfo(page_index) {
	var search_text = $("#search_text").val();
	$.ajax({
		type : "post",
		url : "{:__URL('ADMIN_MAIN/config/helpdocument')}",
		data : { "page_index" : page_index, "page_size" : $("#showNumber").val() },
		success : function(data) {
			if (data["data"].length > 0) {
				$(".table-class tbody").empty();
				for (var i = 0; i < data["data"].length; i++) {
					var html = '';
					html += '<tr align="center">';
					html += '<td><i class="checkbox-common"><input name="sub" type="checkbox" value="'+ data['data'][i]['id']+'"></i></td>';
					
					html += '<td align="left"><input type="text" id="title'+i+'" value="' + data["data"][i]["title"]+ '"onchange="updateDocument('+data["data"][i]["id"]+','+i+','+data["data"][i]["class_id"]+')" class="input-common" /></td>';
					if(data["data"][i]["class_name"]==null){
						html += '<td>-</td>';
					}else{
						html += '<td>'+data["data"][i]["class_name"]+'</td>';
					}
					html += '<td><input type="number" min="0" value="'+ data["data"][i]["sort"]+'" class="num input-common input-common-sort" onchange="updateDocument('+data["data"][i]["id"]+','+i+','+data["data"][i]["class_id"]+')" id="sort'+i+'"></td>';
					html += '<td><a href="'+__URL('ADMIN_MAIN/config/updatedocument?id='+data['data'][i]['id'])+'" >修改</a>&nbsp;&nbsp;<a href="javascript:help_delete('+data['data'][i]['id']+')">删除</a>&nbsp;&nbsp;<a href="'+__URL('SHOP_MAIN/help/index?id='+ data['data'][i]['id']+'&class_id='+data["data"][i]["class_id"])+'" target="_blank">预览</a></td>';
					html += '</tr>';
					$(".table-class tbody").append(html);
				}
			} else {
				var html = '<tr align="center"><td colspan="5">暂无帮助类型</td></tr>';
				$(".table-class tbody").html(html);
			}
			initPageData(data["page_count"],data['data'].length,data['total_count']);
			$("#pageNumber").html(pagenumShow(jumpNumber,$("#page_count").val(),{$pageshow}));
		}
	});
}

//修改input值
function updateDocument(id,index,class_id){
	var title = $("#title"+index).val();
	var sort = $("#sort"+index).val();
	if(title.length == 0){
		showTip("请输入内容","warning");
		return;
	}
	$.ajax({
		type:"post",
		url:"{:__URL('ADMIN_MAIN/config/updateHelpContentTitleAndSort')}",
		data:{ 'id' : id, 'title':title,'sort':sort,"class_id":class_id},
		success: function (data) {
			var flag = "error";
			if(data.code>0) flag = "success";
			LoadingInfo(getCurrentIndex(id,'.table-class tbody'));
			showTip(data['message'],flag);
		}
	});
}

//批量删除
function batchDelete() {
	var id= new Array();
	$(".table-class tbody input[type='checkbox']:checked").each(function() {
		if (!isNaN($(this).val())) {
			id.push($(this).val());
		}
	});
	if(id.length ==0){
		$( "#dialog" ).dialog({
			buttons: {
				"确定,#0059d6,#fff": function() {
					$(this).dialog('close');
				}
			}, 
			contentText:"请选择需要操作的记录",
			title:"消息提醒",
		});
		return false;
	}
	help_delete(id);
}

//删除标题
function help_delete(id){
	$( "#dialog" ).dialog({
		buttons: {
			"确定": function() {
				$(this).dialog('close');
				$.ajax({
					type : "post",
					url: "{:__URL('ADMIN_MAIN/config/titledelete')}",
					data: { "id": id.toString()},
					success : function(data) {
						if(data["code"] > 0){
							LoadingInfo(getCurrentIndex(id,'.table-class tbody'));
							showTip(data["message"],"success");
						}else{
							showTip(data["message"],'error');
						}
					}
				});
			},
			"取消,#f5f5f5,#666": function() {
				$(this).dialog('close');
			}
		},
		contentText:"你确定删除该内容吗？",
		title:"消息提醒"
	});
}
</script>
{/block}